import cv2
import cv2 as cv

# -*- mode: python ; coding: utf-8 -*-


class plotline():
    def __init__(self):
        self.a = []
        self.b = []
        #输出为plot字典，依次记录用户画的点集，构成线的末端点在点集中的索引，线的斜率和截距集，每条线的类型
        self.plot={'pointlist':[],
                   'draw_index':[],
                   'linelist':[],
                   'typelist':[]
                }
        self.flag=0
        #typeflag为记录边界类型的flag
        self.tpyeflag=0
    #画图函数
    def drawborder(self,image):

        self.img = image
        self.imgdraw = self.img.copy()
        cv.namedWindow("Please draw your border in the image")
        cv.setMouseCallback("Please draw your border in the image", self.on_EVENT_LBUTTONDOWN)
        cv.imshow("Please draw your border in the image", self.imgdraw)
        cv.waitKey(0)

    def on_EVENT_LBUTTONDOWN(self, event, x, y, flags, param):
        #鼠标中键改变边界类型
        if event==cv.EVENT_MOUSEWHEEL:
            if len(self.plot['linelist']) and self.flag==0:
                if self.tpyeflag==2:
                    self.tpyeflag=0
                else:
                    self.tpyeflag = self.tpyeflag + 1
                #如果tpyeflag为0则为蓝色边界其他以此类推
                if self.tpyeflag==0:
                        cv.line(self.imgdraw, self.plot['pointlist'][len(self.plot['pointlist']) - 1], self.plot['pointlist'][len(self.plot['pointlist']) - 2],
                                (255, 0, 0), thickness=3)
                        #将边界类型存放进tpyelist
                        self.plot['typelist'][len(self.plot['linelist'])-1]=0
                if self.tpyeflag==1:
                        cv.line(self.imgdraw, self.plot['pointlist'][len(self.plot['pointlist']) - 1], self.plot['pointlist'][len(self.plot['pointlist']) - 2],
                                (0, 255, 0), thickness=3)
                        self.plot['typelist'][len(self.plot['linelist']) - 1] = 1
                if self.tpyeflag==2:
                        cv.line(self.imgdraw, self.plot['pointlist'][len(self.plot['pointlist']) - 1], self.plot['pointlist'][len(self.plot['pointlist']) - 2],
                                (0, 0, 255), thickness=3)
                        self.plot['typelist'][len(self.plot['linelist']) - 1] = 2
                cv.imshow("Please draw your border in the image", self.imgdraw)
        #按下鼠标右键重画，清空一切
        if event==cv.EVENT_RBUTTONDOWN:
            self.imgdraw = self.img.copy()
            self.plot['pointlist'].clear()
            self.plot['draw_index'].clear()
            self.plot['linelist'].clear()
            self.plot['typelist'].clear()
            cv.imshow("Please draw your border in the image", self.imgdraw)
        #鼠标中键切断下一个点和上一个点的联系使得这两点不产生线
        if event==cv.EVENT_MBUTTONDOWN:
            self.flag=1
        #鼠标左键在图上选点
        elif event == cv.EVENT_LBUTTONDOWN:

            xy = "%d,%d" % (x, y)
            self.plot['pointlist'].append((x,y))
            cv.circle(self.imgdraw, (x, y), 1, (0, 0, 255), thickness=-1)
            cv.putText(self.imgdraw, xy, (x, y), cv.FONT_HERSHEY_PLAIN,
                       1.0, (0, 0, 0), thickness=1)
            if len(self.plot['pointlist']) == 1 or len(self.plot['pointlist'])==0:
                pass
            else:
                #如果按过中键则不画线
                if self.flag==1:
                    self.flag=0
                else:
                    cv.line(self.imgdraw,self.plot['pointlist'][len(self.plot['pointlist'])-1],self.plot['pointlist'][len(self.plot['pointlist'])-2],(255,0,0),thickness=3)
                    k,b=self.getfaline(self.plot['pointlist'][len(self.plot['pointlist'])-1],self.plot['pointlist'][len(self.plot['pointlist'])-2])
                    self.plot['draw_index'].append(len(self.plot['pointlist']) - 1)
                    self.plot['typelist'].append(0)
                    if k > 15 or k<-15:
                        k=x
                        self.plot['linelist'].append((k,))
                    else:
                        self.plot['linelist'].append((k,b))
            cv.imshow("Please draw your border in the image", self.imgdraw)
    #得到直线的斜率和截距
    def getfaline(self,point1, point2):
        if (point2[0] - point1[0])==0:
            k=10000
        else:
            k = (point2[1] - point1[1]) / (point2[0] - point1[0])
        b = point1[1] - k * point1[0]
        return k, b



if __name__ == '__main__':
    drawline=plotline()
    image=cv2.imread('pyqt/images/00000.png')
    drawline.drawborder(image)
    print(drawline.plot['pointlist'])
    print(drawline.plot['linelist'])
    print(drawline.plot['draw_index'])
    print(drawline.plot['typelist'])